/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
package org.bs.web.ui;

import java.util.ArrayList;
import java.util.List;
import org.bs.dao.AbstractFacade;

/**
 *
 * @author gbran
 * @param <Entity>
 */
public abstract class AbstractCatalog<Entity> extends AbstractPageBean {

    private Entity current;
    private List<Entity> entityList = new ArrayList<Entity>();
    public static final String CREATE_ACTION = "C";
    public static final String UPDATE_ACTION = "U";
    public static final String DELETE_ACTION = "D";
    private String action = CREATE_ACTION;

    public void save() {
        try {
            validate();
            getCurrentDAOFacade().save(current);
            initializeBean();
            addInformationMessage("Informacion", "Datos guardados con exito...");
        } catch (Exception e) {
            addErrorMessage("Error", e.getMessage());
            LOGGER.error("Ocurrio la siguiente excepcion al intentar guardar: ", e);
        }
    }

    public void update() {
        try {
            validate();
            getCurrentDAOFacade().update(current);
            initializeBean();
            addInformationMessage("Informacion", "Datos actualizados con exito...");
        } catch (Exception e) {
            addErrorMessage("Error", e.getMessage());
            LOGGER.error("Ocurrio la siguiente excepcion al intentar actualizar: ", e);
        }
    }

    public void delete() {
        try {
            validate();
            getCurrentDAOFacade().delete(current);
            initializeBean();
            addInformationMessage("Informacion", "Datos eliminados con exito...");
        } catch (Exception e) {
            addErrorMessage("Error", e.getMessage());
            LOGGER.error("Ocurrio la siguiente excepcion al intentar eliminar: ", e);
        }
    }

    public void set(Entity current, String action) {
        this.current = current;
        this.action = action;
    }

    public Entity getCurrent() {
        return current;
    }

    public void setCurrent(Entity current) {
        this.current = current;
    }

    public List<Entity> getEntityList() {
        return entityList;
    }

    public void setEntityList(List<Entity> entityList) {
        this.entityList = entityList;
    }

    public String getAction() {
        return action;
    }

    public void setAction(String action) {
        this.action = action;
    }

    protected abstract void validate() throws Exception;

    protected abstract AbstractFacade getCurrentDAOFacade();

    protected abstract void initializeBean();
}
